import pygame

#初始化pygame
pygame.init()

#全屏
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)

#获取屏幕的宽高
width, height = screen.get_size()
print(width, height)

#时钟，用于控制帧率
clock = pygame.time.Clock()

#矩形对象，左上角坐标为(0,0)，宽高为100x100
rect = pygame.Rect(0, 0, 100, 100)
#速度为20
speed = 20

running = True
while running:
    for event in pygame.event.get():
        # 退出事件
        if event.type == pygame.QUIT:
            running = False

    # 获取实时按键状态（支持长按）
    pressed_keys = pygame.key.get_pressed()
    # 如果长按左键
    if pressed_keys[pygame.K_LEFT]:
        rect.x -= speed
    # 如果长按右键
    if pressed_keys[pygame.K_RIGHT]:
        rect.x += speed
    # 如果长按上键
    if pressed_keys[pygame.K_UP]:
        rect.y -= speed
    # 如果长按下键
    if pressed_keys[pygame.K_DOWN]:
        rect.y += speed


    #背景黑色
    screen.fill((0, 0, 0))
    #在屏幕上画矩形，颜色为红色
    pygame.draw.rect(screen, (255, 0, 0), rect)
    # fps
    clock.tick(60)
    #刷新屏幕
    pygame.display.update()

#退出pygame
pygame.quit()